package com.baylor;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Application;
import android.app.KeyguardManager;
import android.os.Process;

/**
 * 
 * @author kTian
 *
 */
public class MyApplication extends Application {
	
	private static MyApplication INSTANCE;
	
	public static MyApplication getInstance() {
		return INSTANCE;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		INSTANCE = this;
	}
	
	
	/**
	 * 判断是否在后台运行
	 * @return
	 */
	public static boolean isBackgroundRunning() {
		ActivityManager activityManager = (ActivityManager) INSTANCE.getSystemService(ACTIVITY_SERVICE);
		KeyguardManager keyguardManager = (KeyguardManager) INSTANCE.getSystemService(KEYGUARD_SERVICE);

		if (activityManager == null) {
			return false;
		}
		// get running application processes
		List<ActivityManager.RunningAppProcessInfo> processList = activityManager.getRunningAppProcesses();
		for (ActivityManager.RunningAppProcessInfo process : processList) {
			if (process.pid == Process.myPid()) {
				boolean isBackground = process.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND
						&& process.importance != RunningAppProcessInfo.IMPORTANCE_VISIBLE;
				boolean isLockedState = keyguardManager.inKeyguardRestrictedInputMode();
				if (isBackground || isLockedState) {
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}
	
}
